import AMapLoader from '@amap/amap-jsapi-loader';
import config from '../config/config';

const AMapInit = (success, fail) => {
    AMapLoader.load({
        key: config.MAP_KEY,
        version: '2.0',
        plugins: [
            'AMap.AutoComplete',
            'AMap.PlaceSearch',
            'AMap.Marker',
            'AMap.Circle',
            'AMap.LngLat',
            'AMap.CircleEditor',
        ],
    })
        .then(AMap => {
            success(AMap);
        })
        .catch(e => {
            fail(e);
        });
};

const newCircle = circleData => {
    const { coordinate, radius = 100, fillColor = 'red', extData = {} } = circleData;
    try {
        const circle = new AMap.Circle({
            center: new AMap.LngLat(coordinate.lng, coordinate.lat), // 圆心位置
            radius: radius, // 圆半径
            fillColor: fillColor, // 圆形填充颜色
            strokeColor: '#fff', // 描边颜色
            strokeWeight: 2, // 描边宽度
            cursor: 'pointer',
            extData: extData,
        });
        return circle;
    } catch (e) {
        return null;
    }
};

export { AMapInit, newCircle };
